/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.stockkeeper.view.dashboard;

import com.toedter.calendar.JCalendar;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Date;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.letmeshare.stockkeeper.component.BillEntryTableModel;
import org.letmeshare.stockkeeper.component.PurchaseEntryTableModel;
import org.letmeshare.stockkeeper.component.PurchaseTableModel;
import org.letmeshare.stockkeeper.component.SaleTableModel;
import org.letmeshare.stockkeeper.purchase.entities.Purchase;
import org.letmeshare.stockkeeper.purchase.service.PurchaseService;
import org.letmeshare.stockkeeper.sale.entities.Bill;
import org.letmeshare.stockkeeper.sale.service.SalesService;

/**
 *
 * @author abin
 */
public class Report extends JPanel{
    private JCalendar start_date,end_date;
    private JButton submitDate;
    private PurchaseService purchaseService;
    private SalesService  salesService;
   private String selectedKey;
    

    public Report() {
        purchaseService= PurchaseService.getInstance();
        salesService= SalesService.getInstance();
        initComponents();
        registerHandler();
        initScreen();
    }
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        typeSelect = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        primary = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        second = new javax.swing.JTable();
        datePanel = new javax.swing.JPanel();        
        start_date=new JCalendar(new Date());
        end_date=new  JCalendar(new Date());
        datePanel.add(new Label("Start date"));
        datePanel.add(start_date);
        datePanel.add(new Label("End date"));
        datePanel.add(end_date);
        submitDate=new JButton();
        submitDate.setText("View Report");
        datePanel.add(submitDate);
        
       // DatePicker startDate=new DatePicker(datePanel);

        jLabel1.setText("Select Report Type :");

        typeSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Purchase Report", "Sales Report" }));

        primary.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(primary);

        second.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(second);

//        javax.swing.GroupLayout datePanelLayout = new javax.swing.GroupLayout(datePanel);
//        datePanel.setLayout(datePanelLayout);
//        datePanelLayout.setHorizontalGroup(
//            datePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGap(0, 349, Short.MAX_VALUE)
//        );
//        datePanelLayout.setVerticalGroup(
//            datePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
//            .addGap(0, 36, Short.MAX_VALUE)
//        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(typeSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(datePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(243, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(typeSelect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(datePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(70, 70, 70)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
    }// </editor-fold>
    // Variables declaration - do not modify
    
    
    private void registerHandler(){
        submitDate.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                initScreen();
                second.setModel(new  SaleTableModel(null));
            }
        });
        primary.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                // throw new UnsupportedOperationException("Not supported yet.");
                if(primary.getSelectedRow()>=0){
               selectedKey=""+primary.getModel().getValueAt(primary.getSelectedRow(), 0);
                System.out.println(selectedKey);
               onPrimarySelect();
                }
               
            }

            public void mousePressed(MouseEvent e) {
               // throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseReleased(MouseEvent e) {
               // throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseEntered(MouseEvent e) {
               // throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseExited(MouseEvent e) {
               // throw new UnsupportedOperationException("Not supported yet.");
            }
        });
    }
    private void initScreen(){
        try {
            if (typeSelect.getSelectedIndex() == 0) {
                primary.setModel(new PurchaseTableModel(purchaseService.getAllPurchasesBetween(start_date.getDate(), end_date.getDate())));
            } else if (typeSelect.getSelectedIndex() == 1) {
                primary.setModel(new SaleTableModel(salesService.getActiveBillsBetween(start_date.getDate(), end_date.getDate())));
            }
        } catch (Exception e) {
              JOptionPane.showMessageDialog(typeSelect, e.getMessage(), "Warning Message",JOptionPane.ERROR_MESSAGE);
        }
         selectedKey="";
    }
    private void onPrimarySelect(){
        try {
            if (typeSelect.getSelectedIndex() == 0) {
                second.setModel(new PurchaseEntryTableModel(purchaseService.getPurchase(selectedKey)));
            } else if (typeSelect.getSelectedIndex() == 1) {
                second.setModel(new BillEntryTableModel(salesService.getBill(selectedKey)));
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(datePanel, e.getMessage(), "Warning", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private javax.swing.JPanel datePanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable primary;
    private javax.swing.JTable second;
    private javax.swing.JComboBox typeSelect;
    // End of variables declaration
    
}
